package cn.knightzz.controller;


import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysMenu;
import cn.knightzz.entity.SysRoleMenu;
import cn.knightzz.service.SysRoleMenuService;
import cn.knightzz.vo.AssignRoleMenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-04-18
 */
@Slf4j
@RestController
public class SysRoleMenuController {

    @Resource
    SysRoleMenuService roleMenuService;

    // 根据角色菜单获取对应的id
    @GetMapping("/menus/menuid/{id}")
    public JsonResult<List<Long>> getTreeMenus(@PathVariable Integer id) {
        // 根据角色id 从 角色菜单表获取所有的菜单数据
        List<SysRoleMenu> menus = roleMenuService.getMenuIdListByRoleId(id);
        List<Long> menuIdList = new ArrayList<>();
        for (SysRoleMenu menu : menus) {
            menuIdList.add(menu.getMenuId());
        }
        // 封装返回数据
        JsonResult<List<Long>> result =new JsonResult<>("200", "菜单获取成功");
        result.setData(menuIdList);
        return result;
    }

    @PostMapping("/roles/{roleId}/rights")
    public JsonResult<Object> assignRoleMenu(@PathVariable("roleId") Long roleId,
                                             @RequestBody AssignRoleMenuVO roleMenuVO){
       log.info("roleId   " +  roleId);
       log.info("roleMenuVO   " +  roleMenuVO.getRids());
       roleMenuService.assignRoleMenu(roleMenuVO.getRids(), roleId);
        // 封装响应数据
        return new JsonResult<>("200" , "授权成功!");
    }
}

